package com.shop.cloud.controller.hongfu;

import java.util.List;

import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.shop.cloud.common.mybatis.BaseController;
import com.shop.cloud.domain.hongfu.Bank;
import com.shop.cloud.domain.hongfu.BankAccount;
import com.shop.cloud.domain.hongfu.SafetyCode;
import com.shop.cloud.filter.CurrentUserId;
import com.shop.cloud.filter.IgnoreAuth;
import com.shop.cloud.model.response.ObjectDataResponse;
import com.shop.cloud.service.hongfu.IBankAccountService;
import com.shop.cloud.service.hongfu.IBankService;
import com.shop.cloud.service.hongfu.ISafetyCodeService;
import com.shop.cloud.util.Page;

/**
 * 提现账户
 * @author Xu Sai
 *
 */
@RestController	
@RequestMapping(value = "api")
public class BankAccountController extends BaseController {

	@Autowired
	private IBankAccountService bankAccountService;
	
	@Autowired
	private IBankService bankService;
	
	@Autowired
	private ISafetyCodeService safetyCodeService;
	
	@IgnoreAuth
	@ApiOperation(value = "获取银行列表", notes = "获取银行列表")
	@RequestMapping(value = "/bankList", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<List<Bank>> bankList(@CurrentUserId Long userId, @RequestBody Bank bank){
		
		List<Bank> data =  bankService.find(bank);
		
		return new ObjectDataResponse<List<Bank>>(data);
	}
	
	@IgnoreAuth
	@ApiOperation(value = "获取银行账户列表", notes = "获取银行账户列表")
	@RequestMapping(value = "/bankAccountList", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<BankAccount>> bankAccountList(@CurrentUserId Long userId, @RequestBody BankAccount bankAccount){
		bankAccount.setUserId(userId);
		bankAccount.setIsbank(0);
		bankAccount.setOrderBy("BANK_ACCOUNT.isDefault asc");
		Page<BankAccount> data = bankAccountService.HandlePage(bankAccount);
		
		
		return new ObjectDataResponse<Page<BankAccount>>(data);
	}
	
	@IgnoreAuth
	@ApiOperation(value = "添加银行账户", notes = "添加银行账户")
	@RequestMapping(value = "/addBankAccount", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> addBankAccount(@CurrentUserId Long userId, @RequestBody BankAccount bankAccount){
		
		bankAccount.setUserId(userId);
		int flag = bankAccountService.handleAccount(bankAccount);
		
		return new ObjectDataResponse<Integer>(flag);
	}
	
	@IgnoreAuth
	@ApiOperation(value = "解绑银行账户", notes = "解绑银行账户")
	@RequestMapping(value = "/updateBankAccount", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> updateBankAccount(@CurrentUserId Long userId, @RequestBody BankAccount bankAccount){
		bankAccount.setUserId(userId);
  	    int  flag = bankAccountService.updateBankAccount(bankAccount);
		
		return new ObjectDataResponse<Integer>(flag);
	}
	
	@IgnoreAuth
	@ApiOperation(value = "修改默认账户", notes = "修改默认账户")
	@RequestMapping(value = "/updateDefaultAccount", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> updateDefaultAccount(@CurrentUserId Long userId, @RequestBody BankAccount bankAccount){
		int  flag = 0;
		//所有账户去掉默认
		BankAccount bankAccount2 = new BankAccount();
		bankAccount2.setIsdefault(1);
		bankAccount2.setUserId(userId);
		bankAccountService.HandleUpdate(bankAccount2);
		bankAccount.setIsdefault(0);
		flag = bankAccountService.update(bankAccount);
		
		return new ObjectDataResponse<Integer>(flag);
	}
	
	@IgnoreAuth
	@ApiOperation(value = "设置安全码", notes = "设置安全码")
	@RequestMapping(value = "/handleCode", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> handleCode(@CurrentUserId Long userId, @RequestBody SafetyCode safetyCode){
		
		int data = safetyCodeService.handleCode(userId, safetyCode);
		
		return new ObjectDataResponse<Integer>(data);
	}
	
	@IgnoreAuth
	@ApiOperation(value = "校验安全码", notes = "校验安全码")
	@RequestMapping(value = "/handleCheckCode", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> handleCheckCode(@CurrentUserId Long userId, @RequestBody SafetyCode safetyCode){
		
		int data = safetyCodeService.handleCheckCode(userId, safetyCode);
		
		return new ObjectDataResponse<Integer>(data);
	}
	
	@IgnoreAuth
	@ApiOperation(value = "查询安全码", notes = "查询安全码")
	@RequestMapping(value = "/handleQueryCode", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> handleQueryCode(@CurrentUserId Long userId, @RequestBody SafetyCode safetyCode){
		
		int data = safetyCodeService.handleQueryCode(userId, safetyCode);
		
		return new ObjectDataResponse<Integer>(data);
	}
}
